import 'dart:io';
import 'dart:typed_data';

import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:tender_flutter/utils/toast.dart';

/// 判断字符串是否为空
bool isEmpty(String value) {
  if (value == null || value.isEmpty || value == "null") {
    return true;
  }
  return false;
}

/// 获取安全字符串
String safeStr(String value) {
  if (value == null || value.isEmpty || value == "null") {
    return "";
  }
  return value;
}

///数组转字符串
String getListStr(List list) {
  if (list == null) {
    return "";
  }
  String str = list.toString();
  str.replaceAll("[", "");
  str.replaceAll("]", "");

  return str;
}

/// 保存图片
Future<void> saveImage(Uint8List bytes) async {
  if (Platform.isIOS) {
    var status = await Permission.photos.status;
    if (status.isUndetermined) {
      Map<Permission, PermissionStatus> statuses = await [
        Permission.photos,
      ].request();
      saveImage(bytes);
    }
    if (status.isGranted) {
      final result =
          await ImageGallerySaver.saveImage(bytes, quality: 60, name: "hello");
      if (result) {
        ToastUtils.show("保存成功");
      } else {
        ToastUtils.show("保存失败");
      }
    }
    if (status.isDenied) {
      print("IOS拒绝");
    }
  } else {
    if (Platform.isAndroid) {
      var status = await Permission.storage.status;
      if (status.isUndetermined) {
        Map<Permission, PermissionStatus> statuses = await [
          Permission.storage,
        ].request();
        saveImage(bytes);
      }
      if (status.isGranted) {
        print("Android已授权");
        final result = await ImageGallerySaver.saveImage(bytes, quality: 60);
        if (result != null) {
          ToastUtils.show("保存成功");
        } else {
          ToastUtils.show("保存失败");
        }
      }
      if (status.isDenied) {
        print("Android拒绝");
      }
    }
  }
}
